// litvalues.cpp
// Customizer
// Protea Customizer / Business Logic for managing literal values
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "customizer.h"

//////////////////////////////////////////////////////////////////////////////
//
// LITERAL_VALUES Transactions
//
//////////////////////////////////////////////////////////////////////////////

//@ SEC 2
//@ CHA Add Literal Value
//@ COD AddLiteralValue
//@ NUM 26
//@ CRI LITERAL_NAME
//@ CRI LITERAL
//@ CRI LANG_ID
//@ TAB LITERAL_VALUES Write
//
//@ GEN Add Literal Value will add a literal value for an application
//
TRANSACTION_CODE( AddLiteralValue )
{
	XCBuffer* literal_defn = NULL;
	XCBuffer* literal_value = NULL;

	//@ GRS Start the transaction
	START_TXN( AddLiteralValue );

		//@ Create Literal Defn buffer
		literal_defn = App->CreateBuffer( FMT_LITERAL_DEFN );

		//@ Create the LITERAL_VALUES buffer
		literal_value = App->CreateBuffer( FMT_LITERAL_VALUES );

		//@ Move screen info to transaction buffer
		literal_defn->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		literal_value->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();

			literal_defn->DebugDump();

			//@ SUB LiteralDefnVerify to verify that literal exists
			LiteralDefnVerify( literal_defn, &txnvars, VERIFY_REVIEW, false );

			literal_defn->DebugDump();

			//@ Move corresponding values from LITERAL_DEFN to LITERAL_VALUE
			literal_defn->MoveField( literal_value, FLD_LITERAL );
			literal_value->MoveCorresponding( literal_defn, txnvars.Errors );

			//@ SUB LiteralValueVerify to verify that literal value record is ok
			LiteralValueVerify( literal_value, &txnvars, VERIFY_ADD );

			//@ Check for errors
			txnvars.Errors->Check();

			//@ SUB LiteralValueAdd to add the new application
			LiteralValueAdd( literal_value, &txnvars );

			//@ Determine if literal_defn record needs to be updated...
			int len = literal_value->GetField( FLD_LITERAL )->CalcStringSize();
			XCField* lit_length = literal_defn->GetField( FLD_LITERAL_LENGTH );

			if (lit_length->GetShort() < len) {
				lit_length->SetShort( len );
				LiteralDefnChange( literal_defn, NULL, &txnvars );
			};

			LOG_TRANSACTION();

		END_DB_TXN();

		txnvars.buffer->MoveCorresponding( literal_value, txnvars.Errors );
		App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

	CLEANUP_TXN();
		App->CleanupObjects( 2, &literal_defn, &literal_value );
	END_TXN();
	//@ GRE End the transaction
};





//@ SEC 2
//@ CHA Del Literal Value
//@ COD DelLiteralValue
//@ NUM 26
//@ CRI LITERAL_NAME
//@ CRI LITERAL
//@ CRI LANG_ID
//@ TAB LITERAL_VALUES Write
//
//@ GEN Add Literal Value will add a literal value for an application
//
TRANSACTION_CODE( DelLiteralValue )
{
	XCBuffer* literal_defn = NULL;
	XCBuffer* literal_values = NULL;

	//@ GRS Start the transaction
	START_TXN( DelLiteralValue );

		//@ Create Literal Defn buffer
		literal_defn = App->CreateBuffer( FMT_LITERAL_DEFN );

		//@ Create the LITERAL_VALUES buffer
		literal_values = App->CreateBuffer( FMT_LITERAL_VALUES );

		//@ Move screen info to transaction buffer
		literal_defn->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		literal_values->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();

			//@ SUB LiteralDefnVerify to verify that literal exists
			LiteralDefnVerify( literal_defn, &txnvars, VERIFY_REVIEW, false );

			//@ MoveCorresponding from LITERAL_DEFN to LITERAL_VALUES
			literal_values->MoveCorresponding( literal_defn, txnvars.Errors );

			//@ SUB LiteralValueVerify to verify that literal value record is ok
			LiteralValueVerify( literal_values, &txnvars, VERIFY_DELETE );

			//@ Check for errors
			txnvars.Errors->Check();

			//@ SUB LiteralValueDelete to add the new application
			LiteralValueDelete( literal_values, &txnvars );

			LOG_TRANSACTION();

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 2, &literal_defn, &literal_values );
	END_TXN();
	//@ GRE End the transaction
};





//@ SEC 2
//@ CHA Chg Literal Value
//@ COD ChgLiteralValue
//@ NUM 26
//@ CRI LITERAL_NAME
//@ CRI LITERAL
//@ CRI LANG_ID
//@ TAB LITERAL_VALUES Write
//
//@ GEN Chg Literal Value will alter a literal value for an application
//
TRANSACTION_CODE( ChgLiteralValue )
{
	XCBuffer* literal_defn = NULL;
	XCBuffer* literal_value = NULL;
	XCBuffer* literal_value_new = NULL;

	//@ GRS Start the transaction
	START_TXN( ChgLiteralValue );

		//@ Create Literal Defn buffer
		literal_defn = App->CreateBuffer( FMT_LITERAL_DEFN );

		//@ Move screen info to transaction buffer
		literal_defn->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		//@ Create a LITERAL_VALUE buffer
		literal_value = App->CreateBuffer( FMT_LITERAL_VALUES );

		START_DB_TXN();

			//@ SUB LiteralDefnVerify to verify that literal exists
			LiteralDefnVerify( literal_defn, &txnvars, VERIFY_REVIEW, false );

			//@ Move corresponding information from LITERAL_DEFN to LITERAL_VALUE
			literal_value->MoveCorresponding( literal_defn, txnvars.Errors );

			//@ Move screen info to LITERAL_VALUE buffer
			literal_value->MoveCorresponding( txnvars.buffer, txnvars.Errors );

			//@ SUB LiteralValueVerify to verify that literal value record is ok
			literal_value_new = literal_value->Copy();
			LiteralValueVerify( literal_value, &txnvars, VERIFY_CHANGE );

			//@ Check for errors
			txnvars.Errors->Check();

			//@ SUB LiteralValueChange to alter the literal value record
			LiteralValueChange( literal_value_new, &txnvars, literal_value );

			LOG_TRANSACTION();

		END_DB_TXN();

		txnvars.buffer->MoveCorresponding( literal_value_new, txnvars.Errors );
		App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

	CLEANUP_TXN();
		App->CleanupObjects( 3, &literal_defn, &literal_value, &literal_value_new );
	END_TXN();
	//@ GRE End the transaction
};
